package com.cincc.cinccimdemo.ui.conversationlist;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.ConversationListAdapter;
import com.cincc.cinccimdemo.common.MyUtils;
import com.cincc.cinccimdemo.common.callback.OnConversationInfoUpdateListener;
import com.cincc.cinccimdemo.common.callback.OnConversationItemClickListener;
import com.cincc.cinccimdemo.common.callback.OnConversationListRefreshListener;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.ui.conversation.ConversationActivity;
import com.cincc.cinccimdemo.viewmodel.ConversationListViewModel;
import com.cincc.cinccimdemo.viewmodel.ConversationViewModel;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.core.SipCoreUtils;
import com.cincc.im.listener.ImHisMessageCompleteListener;
import com.cincc.im.listener.ImRcvGroupNotifyListener;
import com.cincc.im.listener.ImRcvMsgListener;
import com.cincc.im.model.dbmodel.ConversationInfo;
import com.cincc.im.model.dbmodel.GroupMessageInfo;
import com.cincc.im.model.dbmodel.P2PMessageInfo;
import com.cincc.im.model.enums.ImSendType;
import com.cincc.im.model.notify.group.GroupNotify;
import com.cincc.im.util.IMUtil;
import com.cincc.im.util.ImManager;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.util.List;

public class ConversationListFragment extends Fragment implements ImRcvMsgListener,
        ImHisMessageCompleteListener, OnConversationListRefreshListener, OnConversationInfoUpdateListener, ImRcvGroupNotifyListener {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_conversation_list, container, false);
        initView(view);
        bindEvent();
        return view;
    }

    RecyclerView recyclerView;
    SwipeRefreshLayout swipeRefreshLayout;
    ConversationListViewModel conversationListViewModel;
    ConversationListAdapter conversationListAdapter;

    private void initView(View view) {
        recyclerView = view.findViewById(R.id.conversation_list_recycler);
        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        conversationListViewModel = new ViewModelProvider(requireActivity()).get(ConversationListViewModel.class);
        conversationListViewModel.conversationListLiveData().observe(getViewLifecycleOwner(), list -> conversationListAdapter.setConversationList(list));
        conversationListAdapter = new ConversationListAdapter(requireActivity());
        conversationListAdapter.setOnItemClickListener((v, position, conversationInfo) -> showPopup(conversationInfo));
        recyclerView.setAdapter(conversationListAdapter);

        swipeRefreshLayout.setOnRefreshListener(() -> {
            conversationListViewModel.reloadConversationList();
            swipeRefreshLayout.setRefreshing(false);
        });
    }

    private void showPopup(ConversationInfo conversationInfo) {
        String item1 = "删除会话";
        String item2 = conversationInfo.isTop ? "取消置顶" : "置顶";
        String item3 = conversationInfo.unreadNum > 0 ? "设为已读" : "设为未读";
        new XPopup.Builder(getActivity()).asCenterList(null, new String[]{item1, item2, item3},
                        (position, text) -> {
                            if (position == 0) {
                                deleteConversation(conversationInfo);
                            }
                            if (position == 1) {
                                setConversationTop(conversationInfo);
                            }
                            if (position == 2) {
                                setConversationRead(conversationInfo);
                            }
                        })
                .show();
    }

    private void deleteConversation(ConversationInfo conversationInfo) {
        IMSipPhoneCtrl.getInstance().deleteConversationById(conversationInfo.type, conversationInfo.targetId);
        conversationListAdapter.deleteConversation(conversationInfo);
    }

    private void setConversationTop(ConversationInfo conversationInfo) {
        IMSipPhoneCtrl.getInstance().setTop(conversationInfo.type, conversationInfo.targetId, !conversationInfo.isTop);
        conversationListViewModel.reloadConversationList();
    }

    private void setConversationRead(ConversationInfo conversationInfo) {
        if (conversationInfo.unreadNum > 0) {
            IMSipPhoneCtrl.getInstance().clearUnreadNum(conversationInfo.type, conversationInfo.targetId);
        } else {
            IMSipPhoneCtrl.getInstance().setMessageUnread(conversationInfo.type, conversationInfo.targetId);
        }
        conversationListAdapter.setConversationRead(conversationInfo, conversationInfo.unreadNum > 0 ? 0 : 1);
    }

    private void bindEvent() {
        SipCoreUtils.getInstance().addImRcvMsgListener(this);
        SipCoreUtils.getInstance().addImHisMessageCompleteListener(this);
        SipCoreUtils.getInstance().addImRcvGroupNotifyListener(this);
        MyUtils.addOnConversationListRefreshListener(this);
        MyUtils.addOnConversationInfoUpdateListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SipCoreUtils.getInstance().removeImMsgListener(this);
        SipCoreUtils.getInstance().removeImHisMessageCompleteListener(this);
        SipCoreUtils.getInstance().removeImRcvGroupNotifyListener(this);
        MyUtils.removeOnConversationListRefreshListener(this);
        MyUtils.removeOnConversationInfoUpdateListener(this);
    }

    @Override
    public void onReceiveP2PMsg(P2PMessageInfo message) {
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void onReceiveGroupMsg(GroupMessageInfo message) {
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void onFetchHisSingleMessageComplete() {
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void onFetchHisGroupMessageComplete() {
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void refreshConversationList() {
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void onConversationUnreadClear() {
        conversationListViewModel.reloadConversationList();
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void onConversationDraftUpdate() {
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void onConversationSilentUpdate() {
        conversationListViewModel.reloadConversationList();
    }

    @Override
    public void onReceiveGroupNotify(GroupNotify message) {
        conversationListViewModel.reloadConversationList();
    }
}
